/*
 * Copyright (C) 2007,2008  Jonathan Hendler
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * 
 * The authors can be contacted at: 
 * @author Jonathan Hendler [jonathan DOT hendler AT google DOT com]
 * 
 * @license GPLv2
 */

/**
 * bench
 * 
 * */
#include "bench.h"


char nymrl_test_get_char(){
	char input = getc(stdin);
	return (input >0) ? input : 'n';
}
 
void n_coding(){
	 const char * original = "010300745#^$%&*()ASKFHJGASFU$%^&*h===%%%&&&   •†©¥©˚≈øˆ…ƒ˚∂†¥ˆß¥¥†¥©©˙˚øˆ†ˆ•¶∞•¶¶•¶¶§•ªˆ¨¥†ˆ¨¥†®´"
	 //math symbols and control cars
	 "∑øπ“ß∂ƒ©˙∆˚¬¬Ω≈ç√∫˜∫√√çç≈Ω≈≈≈\n\n\t\t   --__<><>gajdsfjasdf"
	 //chinese symbols
	 "新 闻网 页贴 吧知 道MP3图 片 帮助 高级 空间 | 更多";
	
	// printf("\n orig %s\n",original);
	 int new_length;
	 char * encoded = nymrl_raw_url_encode(original, strlen(original), &new_length);
	// printf("\n encoded %s\n",encoded);
	 
	 nymrl_raw_url_decode(encoded,  strlen(encoded) );
	 
	// printf("\n encoded decoded %s\n",encoded);
	 
	 if (strcmp(encoded,original) != 0){
		printf("\n+++ raw urlencoding failed +++ \n\n");
	} else {
		printf("\n rawurl encode/decode matched!");
	}
	 
	 const char * simple_encode = "Hello, %100 my name isn't fred. Please stop by. </end>";
	 char * base64enc1 = nymrl_base64_encode(simple_encode);
     char * base64dec1 = nymrl_base64_decode(base64enc1);
////	 
	if (strcmp(base64dec1,simple_encode) != 0){
		printf("\n+++ base 64 failed +++ \n\n");
	} else {
		printf("\n SIMPLE base64 encode/decode matched!");
	}
	 
	 
	// char * encoded2 = nymrl_raw_url_encode(original,strlen(original), &new_length);
	 char * base64enc = nymrl_base64_encode(original);
  	 char * base64dec = nymrl_base64_decode(base64enc); 
//	 char * final_dec =  nymrl_raw_url_decode(base64dec,strlen(base64dec));
//	
    // printf("\n\n%s\n\n%s\nenc\n%s\ndec\n%s\nfinal\n%s\n\n",original,encoded2,base64enc,base64dec,final_dec);
	 
	 if (strcmp(original,base64dec) != 0){
		printf("\n+++ COmplex base64dec  failed +++ \n%s\n\n%s \n",original,base64dec);
	} else {
		printf("\n Complex base64dec encode/decode matched!");
	} 
	 
//   	if (strcmp(final_dec,original) != 0){
//		printf("\n+++ FINAL base64dec urlencoding failed +++ \n\n");
//	} else {
//		printf("\n FINAL  base64dec encode/decode matched!");
//	} 
   	
   	
   	
   	
	 free(encoded);
	// free(encoded2);
	 //free(base64enc);
	// free(base64dec);
	
}

int main(){
	printf("\n starting tests\n");
    NYMRL_TIMER_INIT;
	NYMRL_TIMER_START;
	char * sql; 
	sqlite3 *  db = nymrl_get_router_db();
	int drop_tables = 0;
	nyml_setup_self(drop_tables);
	int __i = 1234;
	int i;
	
	const char * config_key = "cutest_key";
	int config_read_count = 4478;
	char * returned_value;
	
	
	for (i=0;i<__i;++i){
		sql =  nymrl_sqlite_create_sql("SELECT url,nid,rel from node_x_url LIMIT 1");
		NYMRL_SQLITE_STEP_ITERATE(column_value,db,sql,j) 
		column_value =  sqlite3_column_text(stmt, j);
	    //fprintf(stdout, "\tresult '%s' ", );
	    NYMRL_SQLITE_STEP_ITERATE_CLOSE;
	    NYMRL_SQLITE_STEP_ITERATE_CLEANUP;
	}
	
	NYMRL_TIMER_END; 
	NYMRL_TIMER_PRINT("Finished column read of ",__i);
	
	
	NYMRL_TIMER_START;
	for (i=0;i< config_read_count;++i){
		returned_value = nyml_get_config(config_key);
		 //fprintf(stdout, "\t%s", config_key);
		//printf(".");
		
	}
	
	//free(returned_value);
	NYMRL_TIMER_END; 
	NYMRL_TIMER_PRINT("Finished config read of",config_read_count);
	
	NYMRL_TIMER_START;
		n_coding();
	NYMRL_TIMER_END; 
	NYMRL_TIMER_PRINT("Finished encoding",1);
	
	NYMRL_FINISH_ALL;
	return 0;
}

